package com.niji.littly_donkey_android.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import com.android.volley.*;
import com.android.volley.Response.ErrorListener;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.niji.littly_donkey_android.R;
import com.niji.littly_donkey_android.adapter.CommentAdapter;
import com.niji.littly_donkey_android.adapter.CommentPhotosAdapter;
import com.niji.littly_donkey_android.adapter.MyHomeVideoAdapter;
import com.niji.littly_donkey_android.bean.Comment;
import com.niji.littly_donkey_android.bean.Fans;
import com.niji.littly_donkey_android.bean.Photo;
import com.niji.littly_donkey_android.http.ConnectViaSession;
import com.niji.littly_donkey_android.util.JsonUtil;
import com.niji.littly_donkey_android.util.StatusBarUtil;
import com.niji.littly_donkey_android.util.TimeUtil;
import com.niji.littly_donkey_android.util.UrlUtil;
import com.niji.littly_donkey_android.view.StarRatingView;
import org.json.JSONException;

import java.util.*;

public class CommentActivity extends BaseActivity {

    private static final String TAG = "CommentActivity";

    /**
     * 返回
     */
    private Button comment_return;
    /***
     * 星星
     */
    private StarRatingView comment1_star;

    /**
     * 星星数
     */
    private int count;

    private static Integer attractions_id = 0;

    private ListView comment_list;

    private CommentAdapter adapter;

    private RelativeLayout comment_list_loading;


    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.comment1);
        commentReturn();
        changeCommentType();
        init();
    }

    /**
     * 改变评论类型
     */
    private void changeCommentType() {
        Button comment_all_btn = findViewById(R.id.comment_all_btn);
        Button comment_good_btn = findViewById(R.id.comment_good_btn);
        Button comment_middle_btn = findViewById(R.id.comment_middle_btn);
        Button comment_bad_btn = findViewById(R.id.comment_bad_btn);
        //全部评论
        comment_all_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                comment_all_btn.setTextColor(Color.parseColor("#ff4258d0"));
                comment_good_btn.setTextColor(Color.parseColor("#ff333333"));
                comment_middle_btn.setTextColor(Color.parseColor("#ff333333"));
                comment_bad_btn.setTextColor(Color.parseColor("#ff333333"));
                comment_all_btn.setBackground(getDrawable(R.drawable.comment1_butten1));
                comment_good_btn.setBackground(getDrawable(R.drawable.comment1_button2));
                comment_middle_btn.setBackground(getDrawable(R.drawable.comment1_button2));
                comment_bad_btn.setBackground(getDrawable(R.drawable.comment1_button2));
                loadingData(0);
            }
        });

        //好评
        comment_good_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                comment_all_btn.setTextColor(Color.parseColor("#ff333333"));
                comment_good_btn.setTextColor(Color.parseColor("#ff4258d0"));
                comment_middle_btn.setTextColor(Color.parseColor("#ff333333"));
                comment_bad_btn.setTextColor(Color.parseColor("#ff333333"));
                comment_all_btn.setBackground(getDrawable(R.drawable.comment1_button2));
                comment_good_btn.setBackground(getDrawable(R.drawable.comment1_butten1));
                comment_middle_btn.setBackground(getDrawable(R.drawable.comment1_button2));
                comment_bad_btn.setBackground(getDrawable(R.drawable.comment1_button2));
                loadingData(1);
            }
        });

        //中评
        comment_middle_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                comment_all_btn.setTextColor(Color.parseColor("#ff333333"));
                comment_good_btn.setTextColor(Color.parseColor("#ff333333"));
                comment_middle_btn.setTextColor(Color.parseColor("#ff4258d0"));
                comment_bad_btn.setTextColor(Color.parseColor("#ff333333"));
                comment_all_btn.setBackground(getDrawable(R.drawable.comment1_button2));
                comment_good_btn.setBackground(getDrawable(R.drawable.comment1_button2));
                comment_middle_btn.setBackground(getDrawable(R.drawable.comment1_butten1));
                comment_bad_btn.setBackground(getDrawable(R.drawable.comment1_button2));
                loadingData(2);
            }
        });

        //差评
        comment_bad_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                comment_all_btn.setTextColor(Color.parseColor("#ff333333"));
                comment_good_btn.setTextColor(Color.parseColor("#ff333333"));
                comment_middle_btn.setTextColor(Color.parseColor("#ff333333"));
                comment_bad_btn.setTextColor(Color.parseColor("#ff4258d0"));
                comment_all_btn.setBackground(getDrawable(R.drawable.comment1_button2));
                comment_good_btn.setBackground(getDrawable(R.drawable.comment1_button2));
                comment_middle_btn.setBackground(getDrawable(R.drawable.comment1_button2));
                comment_bad_btn.setBackground(getDrawable(R.drawable.comment1_butten1));
                loadingData(3);
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.comment1;

    }
    @Override
    protected int getStatusBarColor() {
        return R.color.white_easy_photos;
    }

    private void init() {
        Intent intent = getIntent();
        attractions_id = intent.getIntExtra("attractions_id", 0);
        showCommentSum();
        loadingData(0);
    }

    /**
     * 显示评论数量
     */
    private void showCommentSum() {
        Button comment_all_btn = findViewById(R.id.comment_all_btn);
        Button comment_good_btn = findViewById(R.id.comment_good_btn);
        Button comment_middle_btn = findViewById(R.id.comment_middle_btn);
        Button comment_bad_btn = findViewById(R.id.comment_bad_btn);
        String url = UrlUtil.URL + "/attractions/comment/types?id=" + attractions_id;
        RequestQueue requestQueue = Volley.newRequestQueue(CommentActivity.this);
        StringRequest stringRequest = new StringRequest(Request.Method.GET,url,new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    Map<String, Object> map = JsonUtil.jsonToMap(s);
                    Integer code = (Integer) map.get("code");
                    if (code == 0) {
                        Map<String,Object> dataMap = (Map) map.get("data");
                        Log.i(TAG,dataMap.toString());
                        Integer all = (Integer) dataMap.get("all");
                        Integer good = (Integer) dataMap.get("good");
                        Integer middle = (Integer) dataMap.get("middle");
                        Integer bad = (Integer) dataMap.get("bad");
                        comment_all_btn.setText("全部 " + all);
                        comment_good_btn.setText("好评 " + good);
                        comment_middle_btn.setText("中评 " + middle);
                        comment_bad_btn.setText("差评 " + bad);
                    } else if(code == -1){
                        Toast.makeText(CommentActivity.this, "登录失效，请重新登录", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(CommentActivity.this, GetPhoneCodeActivity.class);
                        intent.putExtra("loginCode","-1");
                        startActivity(intent);
                        finish();
                    } else{
                        Object message = map.get("message");
                        Toast.makeText(getApplicationContext(), message.toString(), Toast.LENGTH_SHORT).show();
                        overridePendingTransition(R.anim.xpage_slide_in_left, R.anim.xpage_slide_out_right);
                        finish();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                volleyError.getMessage();
            }
        }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                String sesseionid = new ConnectViaSession(CommentActivity.this).GetSession();
                if (!sesseionid.equals("")) {
                    HashMap<String, String> headers = new HashMap<String, String>();
                    headers.put("cookie", sesseionid);
                    return headers;
                } else {
                    return super.getHeaders();
                }
            }
        };
        requestQueue.add(stringRequest);
    }

    /**
     * 初始化加载数据
     */
    private void loadingData(int type){
        comment_list = findViewById(R.id.comment_list);
        comment_list_loading = findViewById(R.id.comment_list_loading);
        comment_list_loading.setVisibility(View.VISIBLE);
        ArrayList<Comment> commentList = new ArrayList<>();
        String url = UrlUtil.URL + "/attractions/get/comment?id=" + attractions_id + "&type=" + type;
        RequestQueue requestQueue = Volley.newRequestQueue(CommentActivity.this);
        StringRequest stringRequest = new StringRequest(Request.Method.GET,url,new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    Map<String, Object> map = JsonUtil.jsonToMap(s);
                    Integer code = (Integer) map.get("code");
                    if (code == 0) {
                        Map<String,Object> dataMap = (Map) map.get("data");
                        Log.i(TAG,dataMap.toString());
                        List<Map<String, Object>> mapList = (List<Map<String, Object>>) dataMap.get("data");
                        Log.i(TAG,mapList.toString());
                        for (int i = 0; i < mapList.size(); i++) {
                            Comment comment = new Comment();
                            Integer id = (Integer) mapList.get(i).get("id");
                            Integer user_id = (Integer) mapList.get(i).get("user_id");
                            Integer score = (Integer) mapList.get(i).get("score");
                            String content = (String) mapList.get(i).get("content");
                            Integer recommend = (Integer) mapList.get(i).get("recommend");
                            String userNickname = (String) mapList.get(i).get("userNickname");
                            String created_at = (String) mapList.get(i).get("created_at");
                            String userAvatar = (String) mapList.get(i).get("userAvatar");
                            List<String> photos = (List<String>) mapList.get(i).get("photos");
                            comment.setId(id);
                            comment.setTitle(userNickname);
                            comment.setMessage(content);
                            comment.setTime(created_at);
                            comment.setRate(score);
                            comment.setHead(UrlUtil.URL  + userAvatar);
                            comment.setPhotos(photos);
                            commentList.add(comment);
                        }
                    } else if(code == -1){
                        Toast.makeText(CommentActivity.this, "登录失效，请重新登录", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(CommentActivity.this, GetPhoneCodeActivity.class);
                        intent.putExtra("loginCode","-1");
                        startActivity(intent);
                        finish();
                    } else{
                        Object message = map.get("message");
                        Toast.makeText(getApplicationContext(), message.toString(), Toast.LENGTH_SHORT).show();
                        overridePendingTransition(R.anim.xpage_slide_in_left, R.anim.xpage_slide_out_right);
                        finish();
                    }
                    adapter = new CommentAdapter(CommentActivity.this,commentList);
                    comment_list.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                    comment_list_loading.setVisibility(View.INVISIBLE);
                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                volleyError.getMessage();
            }
        }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                String sesseionid = new ConnectViaSession(CommentActivity.this).GetSession();
                if (!sesseionid.equals("")) {
                    HashMap<String, String> headers = new HashMap<String, String>();
                    headers.put("cookie", sesseionid);
                    return headers;
                } else {
                    return super.getHeaders();
                }
            }
        };
        requestQueue.add(stringRequest);



    }

    /**
     * 登录界面返回
     */
    private void commentReturn() {
        comment_return = findViewById(R.id.comment_return);
        comment_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                overridePendingTransition(R.anim.xpage_slide_in_left,R.anim.xpage_slide_out_right);
                finish();
            }
        });

    }
}
